/**
 * package for all the model classes to fit the mvc format
 */
package javamonsters.model;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.math.BigDecimal;
import java.text.NumberFormat;

/**
 * Represents an inventory item that has a price and a quantity associated with it
 * @author John Ham
 * @version 1.0
 */
public class InventoryItem implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4322251670733927605L;
	
	/**
	 * object output stream
	 */
	private ObjectOutputStream out;
	
	/**
	 * the price of an item
	 */
	private final BigDecimal price;
	
	/**
	 * the quantity of an item
	 */
	private int qty;
	
	/**
	 * format for the numbers being used
	 */
	private final NumberFormat fmt = NumberFormat.getCurrencyInstance();
	
	/**
	 * default setting for an item to not be grayed out
	 */
	private boolean isGray = false;
	
	/**
	 * @param price the price of the item
	 * @param qty the quantity of the item
	 */
	public InventoryItem(BigDecimal price, int qty) {
		this.price = price;
		this.qty = qty;
	}
	
	/**
	 * @param price the price of the item
	 */
	public InventoryItem(BigDecimal price) {
		this.price = price;
		this.qty = 1;
	}

	/**
	 * Gets the price of the TradeGood
	 * 
	 * @return the price of the TradeGood
	 */
	public BigDecimal getPrice() {
		return price;
	}
	
	/**
	 * Gets the quantity of the TradeGood
	 * @return the quantity of the TradeGood
	 */
	public int getQuantity() {
		return qty;
	}
	
	/**
	 * Sets the quantity of the TradeGood
	 * @param qty amount to set the quantity to
	 */
	public void setQuantity(int qty) {
		this.qty = qty;
	}
	
	/**
	 * Printing out the InventoryItem for use in the JList
	 * @return string representation of the InventoryItem
	 */
	public String toString() {
		return fmt.format(price.doubleValue()) + ", " + qty + " available";
	}
	
	/**
	 * @return returns a string representing the price in the correct format
	 */
	public String formatShipItem() {
		return fmt.format(price.doubleValue());
	}
	
	/**
	 * Check for seeing if an entry in the JList is grayed out i.e. cannot be traded
	 * @return true if gray and cannot be traded, false otherwise
	 */
	public boolean isGray() {
		return isGray;
	}
	
	/**
	 * Set the isGray variable
	 * @param isGray true if isGray should be true, false otherwise
	 */
	public void setIsGray(boolean isGray) {
		this.isGray = isGray;
	}
	
	/**
	 * writes this object to a save file
	 * 
	 * @param filename the name of the file to be saved to
	 * @throws IOException
	 */
	public void writeObject(String filename) throws IOException {
		try {
			out = new ObjectOutputStream(new FileOutputStream(filename + ".dat"));
			out.writeObject(this);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			out.close();
		}
	}
}
